<?php

/**
 * Модель модуля Голосования
 */
class PollsModel extends DbModel {

    protected $_name = 'page_poll';

    public function getPolls($sid = 0, $id = 0) {

        $sql = $this->select()->order('active ASC')->order('timestamp DESC');

        if ($sid > 0)
            $sql->where('sid = ?', $sid);

        if ($id > 0) {
            $sql->where('id = ?', $id);
            $res = $this->fetchRow($sql);
        } else {
            $res = $this->fetchAll($sql);
        }

        return $res;
    }

    public function getLastPoll($sid = 0) {
        $sql = $this->select()->order('active ASC')->order('timestamp DESC')->where('active > 0')->limit(1);
        if ($sid > 0)
            $sql->where('sid = ?', $sid);
        $res = $this->fetchRow($sql);
        return $res;
    }

    public function updateQuestion($data, $pid = 0) {
        if ($pid > 0) {
            $this->update($data, 'id = ' . $pid);
            return $pid;
        } else {
            $select = $this->insert($data);
            return $this->_db->lastInsertId();
        }
    }

    public function getAnswers($pid) {
        $sql = $this->_db->select()->from(PREFIX . 'page_poll_choice')->where('pid = ?', $pid)->order('position ASC');
        $res = $this->_db->fetchAll($sql);
        return $res;
    }

    public function addAnswers($data, $pid) {
        foreach ($data as $key => $value) {
            if (trim($value) <> '') {
                $row = array('pid' => (int) $pid, 'answer' => htmlspecialchars($value, ENT_QUOTES), 'position' => (int) $key);
                $this->_db->insert(PREFIX . 'page_poll_choice', $row);
            }
        }
        return true;
    }

    public function updateAnswers($data, $pid) {
        $answers = $this->getAnswers($pid);
        $old_answers = array();
        foreach ($array as $v) {
            $old_answers[$v->id] = $v->answer;
        }
        $this->_db->update(PREFIX . 'page_poll_choice', array('pid' => 0), 'pid = ' . $pid);
        $i = 0;
        foreach ($data as $v) {
            $i++;
            if (isset($old_answers[$v->id])){
                $row = array('id' => $v->id, 'pid' => (int) $pid, 'answer' => htmlspecialchars($v->answer, ENT_QUOTES), 'votes' => $v->votes, 'position' => $i);
                $this->_db->update(PREFIX . 'page_poll_choice', $row, 'id = ' . $v->id);
            } else {
                $row = array('pid' => (int) $pid, 'answer' => htmlspecialchars($v->answer, ENT_QUOTES), 'votes' => $v->votes, 'position' => $i);
                $this->_db->insert(PREFIX . 'page_poll_choice', $row);
            }
        }
        $this->_db->delete(PREFIX . 'page_poll_choice', 'pid = 0');
    }

    public function alreadyVote($pid,$userIP){
        $sql = $this->_db->select()->from(PREFIX . 'page_poll_votes')->where('host = ?', $userIP)->where('pid = ?', $pid);
        $res = $this->_db->fetchAll($sql);
        if (count($res) > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function vote($data) {
        $sql = $this->_db->select()->from(PREFIX . 'page_poll_choice')->where('pid = ?', $data['pid'])->where('id = ?', $data['cid']);
        $res = $this->_db->fetchRow($sql);
        if (!is_null($res)){
            $res->votes++;
            $this->_db->update(PREFIX . 'page_poll_choice', array('votes' => $res->votes), 'id = ' . $data['cid']);
            $this->_db->insert(PREFIX . 'page_poll_votes', $data);
            return $this->_db->lastInsertId();
        } else {
            return false;
        }
    }

    /**
     * Постраничное возвращение голосований
     *
     * @param int $pageNumber Номер страницы
     * @param int $sid #ID секции
     * @return mixed
     */
    public function getPaginatorPolls($pageNumber = 1, $sid = 0) {
        $select = $this->select()->order('active DESC');
        if ($sid > 0) {
            $select->where('sid = ?', $sid);
        }
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setCurrentPageNumber($pageNumber);
        $model = new PaginatorModel();
        return $model->get($paginator);
    }

    /**
     * Удаление ленты голосований
     *
     * @param int $sid #ID секции
     */
    public function deleteSection($sid) {
        $id = (int) $sid;
        if ($id > 0) 
            $this->delete("sid = " . $id);       
    }

}